package vinux.web.common.sso;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;

import vinux.service.common.shiro.ShiroToken;
import vinux.service.common.shiro.manager.TokenManager;
import vinux.service.sso.core.extend.SsoExtend;



public class SsoLogin extends SsoExtend {

	/**
	 * 处理用户信息并且进行登录
	 */
	@Override
	public void LoginProcess(HttpServletRequest request, HttpServletResponse response,
			String userInfo) throws Exception {
		ShiroToken token = new ShiroToken(userInfo);
		token.login();
	}

	/**
	 * ajax 方式退出
	 */
	@Override
	public Map<String, Object> ajaxlogout(HttpServletRequest arg0,
			HttpServletResponse arg1) {
		Map<String, Object> result = new HashMap<String, Object>();
		ShiroToken token = (ShiroToken) SecurityUtils.getSubject().getPrincipal();
		if(null != token){
			result.put("status", true);
			result.put("roleType",token.getRoleType());
			result.put("outyet", "1");
			SecurityUtils.getSubject().logout();
			token = (ShiroToken) SecurityUtils.getSubject().getPrincipal();
			if(null != token){
				SecurityUtils.getSubject().logout();
				//先退出上一个用户
				TokenManager.seToken(null);
			}
		}else{
			result.put("status", true);
			//已经退出过
			result.put("outyet", "0");
		}
		return result;
	}

	/**
	 * 退出当前登录
	 */
	@Override
	public void logoutCurrent() {
		ShiroToken token = (ShiroToken) SecurityUtils.getSubject().getPrincipal();
		if (null != token) {
			SecurityUtils.getSubject().logout();
			//设置缓存为null
			TokenManager.seToken(null);
		}

	}

	/**
	 * 是否已经登录
	 */
	@Override
	public boolean isLogin() {
		ShiroToken token = (ShiroToken) SecurityUtils.getSubject().getPrincipal();
		if (null != token) {
			return true;
		}
		return false;
	}
}
